
    
def haineilogin():
    COOKIEFILE = "/home/todwong/cookie_hainei.lwp"
    cj = cookielib.LWPCookieJar()
    if os.path.isfile(COOKIEFILE): 
        cj.load(COOKIEFILE)
    redirector = urllib2.HTTPRedirectHandler()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), redirector)
    urllib2.install_opener(opener)
    
    f = urllib2.urlopen("http://www.hainei.com")
    #f = opener.open("http://app.hainei.com/sandbox")
    html = f.read()
    print html
    h = re.findall("a=login&h=([^\"]{8})\"", html)
    print h 
    f = urllib2.urlopen("http://www.hainei.com/account?a=login&h=%s" % h[0], "email=notnannotnull%40gmail.com&password=kuaisan&autologin=on")
    print f.read()
    cj.save(COOKIEFILE)

import urllib2
import cookielib
import re
import os.path
import string
class haineiCrawler:
    '''crawler on hainei'''
    
    def __init__(self):
        self.COOKIEFILE = "/home/todwong/cookie_hainei.lwp"
        self.account_email = "notnannotnull%40gmail.com"
        self.account_password = ""
        self.cj = cookielib.LWPCookieJar()
        urllib2.install_opener(urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj), urllib2.HTTPRedirectHandler()))
    
    def reLoginAndSaveCookie(self):
         f = urllib2.urlopen("http://www.hainei.com")
         html = f.read()
         h = re.findall("a=login&h=([^\"]{8})\"", html)
         f = urllib2.urlopen("http://www.hainei.com/account?a=login&h=%s" % h[0], "email=%s&password=%s&autologin=on" % (self.account_email, self.account_password))
         self.cj.save(self.COOKIEFILE)
         
    def loadCookieAndGoto(self, path):
        self.cj.load(self.COOKIEFILE)
        f = urllib2.urlopen(path)
        return f.read()
    
    def iframeSrc(self, html):
        src = re.findall("app-canvas.*?src=\"([^\"]+?)\"", html)
        return src[0]
    
    def sandboxParams(self):
        html = self.loadCookieAndGoto("http://app.hainei.com/sandbox")
        src = self.iframeSrc(html)
        i = src.find("?")
        src = src[i+1:]
        ls = src.split("&")
        m = {}
        for item in ls:
            k, v = item.split("=")
            m[k] = v
        return m
    
if __name__ == "__main__":
    #print "login into hainei..."
    #haineilogin()
    hc = haineiCrawler()
    #hc.reLoginAndSaveCookie()
    print hc.sandboxParams()